/*
 * CleanWebPage.java
 *
 * Created on February 8, 2009, 3:23 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package regnum_web_trade;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 *
 * @author alvaro
 */
public class CleanWebPage {
    private String file;
    private String prefix;
    private String s = System.getProperty("file.separator");
    
    /**
     * Creates a new instance of CleanWebPage
     */
    public CleanWebPage(String prefix, String file) {
        this.prefix = prefix;
        this.file = file;
    }
    
    public void clean(){
        File f = new File(file);
        f.renameTo(new File(file+"2"));
        
        f.delete();
        f = new File(this.file+"2");
        
        File dest = new File(prefix+s+"temp"+s+"trade.html");
        System.out.println("fichero destino: "+dest.getPath());
        
        try{
            boolean primerTableHecho = false;
            boolean empezadoPrimerTable = false;
            
            PrintWriter pw = new PrintWriter(new FileWriter(dest));
            Scanner scan = new Scanner(f);
            String line = null;
            while(scan.hasNextLine()){
                line = scan.nextLine();
                
                if(line.contains("<table "))
                    empezadoPrimerTable = true;
                
                if(line.contains("</table>") && empezadoPrimerTable){
                    empezadoPrimerTable = false;
                    primerTableHecho = true;
                }
                
                
                if(!empezadoPrimerTable || primerTableHecho){
                    System.out.println("write: "+line);
                    pw.println(line);
                }
                
            }
            scan.close();
            pw.flush();
            pw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
